#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from robyn import Robyn, ALLOW_CORS, Request
import logging

from settings.config import settings
from common.log import init_log
from app.admin.api import admin_v1_router
from database.db import init_db

logger = logging.getLogger(__name__)

def make_middlewares(app: Robyn):
    ALLOW_CORS(app=app, origins=settings.CORS_ORIGINS)

async def app_startup_handler():
    if settings.FORMAT_LOG:
        await init_log()
    logger.info("程序启动成功")
    if settings.ENABLE_DB:
        await init_db()
    
async def app_shutdown_handler():
    logger.info("程序关闭")
    
def create_app() -> Robyn:
    app = Robyn(__file__)

    # make_middlewares(app=app)         bug
    app.startup_handler(app_startup_handler)
    app.shutdown_handler(app_shutdown_handler)
    app.include_router(admin_v1_router)
       
    return app
    


